<template>
  <div id="large">
    <div id="headList">
      <div class="logo_nav">
        <router-link to="/">
          <div class="logoHome">
            <div>
              <img src="../../assets/logo.png" alt="" title="网址顶图" />
            </div>
            <h2>{{ $t("home.fishBallsNovel") }}</h2>
          </div>
        </router-link>
      </div>
      <div class="cont_nav">
        <div class="left_nav">
          <div class="nav_li">
            <router-link class="tocont" to="/" exact>{{
              $t("home.home")
            }}</router-link>
          </div>
          {{ facebookUsername }}
          <!-- 作者福利 -->
          <!-- <div class="nav_li">
            <router-link
              style="margin-left: 20px"
              target="_blank"
              class="tocont"
              to="/h5page"
              >分享</router-link
            >
          </div> -->
          <!-- :to="ListuserInfoss == '' ? '/home' : '/author/bookManage'" -->
          <div class="nav_li">
            <router-link
              style="margin-left: 20px"
              target="_blank"
              class="tocont"
              to="/home"
              >{{ $t("home.authorCenter") }}</router-link
            >
          </div>
          <div class="nav_li">
            <router-link class="tocont" to="/authorWelfare">{{
              $t("home.authorWelfare")
            }}</router-link>
          </div>
          <div class="nav_li">
            <router-link class="tocont" to="/downLoad">{{
              $t("home.downloadApp")
            }}</router-link>
          </div>
        </div>
      </div>
      <!-- 登录--首页新增--结束 -->
      <!-- <div class="Ho_login_OK" v-if="userInfo" @click="ONsignout">
        <div class="yi">
          <img :src="userInfo.headimgurl" alt="" />
        </div>
        <div class="er">{{ userInfo.userName }}</div>
        <div class="san">
          <i class="el-icon-arrow-down"></i>
        </div>
      </div> -->
      <!-- 登录--首页新增--开始 -->
      <!-- <div class="Ho_login" v-else>
        <div class="login_title" @click="Clicklogin">Sign in</div>
      </div> -->
      <!-- 退出登录--开始 -->
      <div class="signout_css g_id_signout" v-if="signout" @click="logout">
        <div class="img">
          <img src="../../assets/to_login.png" alt="" />
        </div>
        Log out
        <div class="triangle"></div>
      </div>
      <!-- 退出登录--结束 -->
    </div>

    <!-- 多种登录弹框--开始 -->
    <div class="Login_Pop_Up" v-if="Login_On">
      <div class="close" @click="close">
        <img src="../../assets/close.png" alt="" />
      </div>
      <div class="zhonxin">
        <!-- title标 -->
        <div class="title">Sign in to FishNovel</div>
        <!-- 验证码 -->
        <div
          style="
            width: 33px;
            height: 22px;
            cursor: pointer;
            line-height: 22px;
            position: absolute;
            top: 215px;
            right: 64px;
            z-index: 1;
            color: #f55651;
          "
        >
          <span v-show="show" @click="getCode">GET</span>
          <span
            style="
              width: 50px;
              margin-left: -85px;
              color: #999999;
              pointer-events: none;
            "
            v-show="!show"
            class="count"
            >Reacquire {{ count }} s</span
          >
        </div>
        <!-- input框 -->
        <div class="form-tiele">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="0px"
            class="demo-ruleForm"
          >
            <div style="color: #333333; font-size: 16px">Email</div>
            <el-form-item style="margin-top: 10px" prop="phone">
              <el-input v-model="ruleForm.phone"></el-input>
            </el-form-item>
            <div style="color: #333333; font-size: 16px">Verify Code</div>
            <el-form-item style="margin-top: 10px" prop="code">
              <el-input
                style="position: relative"
                v-model="ruleForm.code"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-button
                style="
                  width: 399px;
                  color: #fff;
                  background: linear-gradient(to right, #ee685a, #e25177);
                  margin-top: 20px;
                "
                @click="submitForm('ruleForm', 6)"
                >Sign in</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="OR">
          <div class="left"></div>
          <div class="in">OR</div>
          <div class="right"></div>
        </div>
        <div class="choice">
          <div class="shang" @click="FacebookOn(8)">
            <div class="left">
              <img src="../../assets/fff.png" alt="" />
            </div>
            <fb-signin-button
              id="signout"
              :params="fbSignInParams"
              @success="onSignInSuccessFB"
              @error="onSignInErrorFB"
            >
              Sign in with Facebook
            </fb-signin-button>
          </div>
          <div class="zhong" @click="GoogleOn(7)">
            <div class="left">
              <img src="../../assets/gool.png" alt="" />
            </div>
            <g-signin-button
              :params="googleSignInParams"
              @success="onSignInSuccess"
              @error="onSignInError"
            >
              Sign in with Google
            </g-signin-button>
          </div>
          <div class="xia">
            <vue-apple-signin
              class="xia"
              color="black"
              :border="false"
            ></vue-apple-signin>
          </div>
        </div>
        <div class="tips">
          Users who are not registered with Fishnovel will be registered
          automatically at login.
        </div>
      </div>
    </div>
    <!-- 多种登录弹框--结束 -->

    <!-- 登录失败提示弹框--开始 -->
    <div class="ON_login" v-if="ON_login_false">
      <div class="neirong">
        <div class="shang">Login failed, please try again.</div>
        <div class="xia" @click="ON_login">OK</div>
      </div>
    </div>
    <!-- 登录失败提示弹框--结束 -->

    <!-- 退出登录气泡--开始 -->
    <div class="popup_is" v-if="popup_is" @click="setpopup_is"></div>
    <!-- 退出登录气泡--结束 -->
  </div>
</template>
<script type="text/javascript">
import base from "@/assets/base.css";
import { VerificationCode, Lonsignin, getCode, userInfo } from "@/api/info";
export default {
  name: "facebook",
  data() {
    var ruleNumber = (rule, value, callback) => {
      var myreg = /^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
      if (value === "") {
        callback(new Error("Please enter email."));
      } else if (!myreg.test(value)) {
        callback(new Error("That is not a valid email address."));
      } else {
        callback();
      }
    };
    return {
      Applecode: "",
      params: "",
      NOuserName: "",
      NOheadimgurl: "",
      ListuserInfo: [],
      ListuserInfoss: [],
      login: true,
      username: this.$t("home.assignedUserName"),
      msgNum: null,
      hide: true, //新增首页--Log in -- 隐藏
      moren: false, //已登陆
      signout: false, //退出登录
      popup_is: false,
      Login_On: false,
      ON_login_false: false,
      googleSignInParams: {
        client_id:
          "139301213659-qqqlitnpvv0sagl7m96agrfbd9lujqs0.apps.googleusercontent.com",
      },
      fbSignInParams: {
        scope: "email,user_likes",
        return_scopes: true,
      },
      googleId: "",
      googleImg: "",
      googleusername: "",
      facebookuserid: "",
      facebookUsername: "",
      regEmail: "",
      is_type: "",
      show: true,
      count: "",
      timer: null,
      nihaoma: "",
      ruleForm: {
        phone: "",
        code: "",
      },

      rules: {
        phone: [{ validator: ruleNumber, trigger: "blur" }],
        code: [
          {
            required: true,
            message: "Please enter the verification code.",
            trigger: "blur",
          },
          {
            min: 4,
            max: 4,
            message: "This is not a valid verification code.",
            trigger: "blur",
          },
        ],
      },
    };
  },
  inject: ["reload"],
  methods: {
    // 谷歌第三方登录--开始
    onSignInSuccess(googleUser) {
      // console.log(googleUser);
      const profile = googleUser.getBasicProfile();
      this.googleusername = profile.hf; //姓名
      this.googleImg = profile.nN; //头像
      this.googleId = profile.GW; //唯一id
      // console.log(profile);
      //调用登录接口
      this.onsubmit();
    },
    onSignInError(error) {
      // console.log(error, "登录失败回调Goole");
    },
    //Google传参
    GoogleOn(e) {
      this.is_type = e;
    },
    //Facebook传参
    FacebookOn(e) {
      this.is_type = e;
    },
    // 谷歌第三方登录--结束
    // Facebook第三方登录--开始
    onSignInSuccessFB(response) {
      // 登录成功回调
      FB.api("/me", (dude) => {
        this.facebookUsername = `${dude.name}`;
      });
      this.facebookuserid = response.authResponse.userID;
      //调用登录接口
      this.onsubmit();
      // console.log(response, "返回第三方的登录信息"); //返回第三方的登录信息 tolen等
    },
    onSignInErrorFB(error) {
      // console.log(error, "登录失败回调Facebook");
      //登录失败回调
    },
    // Facebook第三方登录--结束
    ONsignout() {
      this.signout = true;
      this.popup_is = true;
    },
    setpopup_is() {
      this.signout = false;
      this.popup_is = false;
    },
    // 用户退出
    logout() {
      this.$store.commit("setUserInfo", "");
      localStorage.removeItem("userInfo");
      localStorage.removeItem("token");
      sessionStorage.removeItem("userInfo");
      this.clearAllCookie();
      this.$router.push("/");
      this.reload();
    },
    clearAllCookie() {
      var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
      if (keys) {
        for (var i = keys.length; i--; )
          document.cookie = keys[i] + "=0;expires=" + new Date(0).toUTCString();
      }
    },
    //用户点击登录
    Clicklogin() {
      this.Login_On = true;
    },
    //验证码倒计时
    getCode() {
      let verifys = /^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.count = TIME_COUNT;
        if (this.ruleForm.phone == "") {
          this.show = true;
          return;
        } else if (!verifys.test(this.ruleForm.phone)) {
          this.show = true;
          return;
        } else {
          this.show = false;
          this.sendPhoneMessage();
        }
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
          } else {
            this.show = true;
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000);
      }
    },
    //发送验证码接口
    sendPhoneMessage() {
      VerificationCode(this.ruleForm.phone).then(
        (res) => {},
        (fail) => {}
      );
    },
    //关闭登录
    close() {
      this.Login_On = false;
    },
    // 登录失败
    ON_login() {
      this.ON_login_false = false;
    },
    //登录接口
    onsubmit() {
      if (this.is_type == 6) {
        this.params = {
          code: this.ruleForm.code,
          openid: this.ruleForm.phone,
          type: this.is_type,
        };
      } else if (this.is_type == 7) {
        this.params = {
          openid: this.googleId,
          headImgUrl: this.googleImg,
          nickname: this.googleusername,
          type: this.is_type,
        };
      } else if (this.is_type == 8) {
        this.params = {
          openid: this.facebookuserid,
          type: this.is_type,
        };
      }
      Lonsignin(this.params).then((res) => {
        if (res.code == 1) {
          this.$store.commit("setUserInfo", res.source);
          localStorage.setItem("userInfo", JSON.stringify(res.source));
          sessionStorage.setItem("userInfo", JSON.stringify(res.source));
          //存token
          localStorage.setItem("token", res.source.token);
          this.reload();
        }
      });
    },
    // 点击登录
    submitForm(formName, e) {
      this.is_type = e;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.onsubmit();
        } else {
          return false;
        }
      });
    },
    // 查询apple返回用户信息
    getCodes() {
      getCode(this.Applecode).then(
        (res) => {
          if (res.code == 1) {
            // let userInfos = res.source;
            this.$store.commit("setUserInfo", res.source);
            localStorage.setItem("userInfo", JSON.stringify(res.source));
          }
        },
        (fail) => {
          console.log(fail);
        }
      );
    },
  },
  created() {
    this.Applecode = this.$route.query.code;
    if (this.Applecode != undefined) {
      this.getCodes();
    }
  },
  mounted() {
    history.pushState(null, null, document.URL);
    window.addEventListener("popstate", function () {
      history.pushState(null, null, document.URL);
    });
  },
  computed: {
    bTrue: function () {
      return this.$store.getters.userInfo.token;
    },
    userInfo() {
      return this.$store.getters.userInfo;
    },
  },
};
</script>
<style lang="scss" scoped="scoped">
.popup_is {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 1;
}
.el-form-item {
  margin-bottom: 30px;
}

a.tocont:visited {
  color: #fff;
}
body {
  background-color: #000;
}
.logoHome {
  display: flex;
  color: #3385fd;
  h2 {
    line-height: 85px;
    font-size: 32px;
    margin-left: 15px;
  }
}
i {
  font-size: 22px;
}
.toolbar_user {
  width: 100%;
  li {
    text-align: center;
    color: black;
    padding: 2px 0;
  }
}
.nav_li {
  display: flex;
  flex-direction: column;
  height: 100%;
  justify-content: center;

  a {
    font-size: 18px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    margin: 0 16px;
    text-align: center;
    color: #fff;
  }
  a.tocont.router-link-active {
    color: #3385fd;
    font-weight: 600;
  }
}

#headList {
  width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: row;
  height: 80px;
  position: relative;
  .logo_nav {
    min-width: 250px;
    margin-right: 40px;

    img {
      margin: 15px 0;
    }
  }

  .cont_nav {
    flex: 1;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    color: #fff;
    .left_nav {
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      text-align: center;
    }

    .right_nav {
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      text-align: center;

      .user {
        display: flex;
        flex-direction: row;
        justify-content: space-around;
        text-align: center;
      }
    }
  }
  .Ho_login {
    color: #fff;
    display: flex;
    flex-direction: column;
    justify-content: center;
    .login_title {
      width: 70px;
      height: 25px;
      border: 1px solid #fff;
      border-radius: 5px;
      text-align: center;
      line-height: 25px;
      font-size: 12px;
      cursor: pointer;
    }
  }
  .Ho_login_OK {
    // width: 150px;
    color: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .yi {
      width: 35px;
      height: 35px;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      img {
        width: 100%;
        height: auto;
        border-radius: 50%;
      }
    }
    .er {
      // width: 83px;
      height: 25px;
      // overflow: hidden;
      // text-overflow: ellipsis;
      // white-space: nowrap;
      cursor: pointer;
      text-align: center;
      margin: 0 10px 0 10px;
    }
    .san {
      width: 25px;
      height: 25px;
      // background: #3385FD;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
    }
  }
  .signout_css {
    width: 153px;
    height: 50px;
    background: #fff;
    color: #333333;
    border-radius: 4px;
    position: absolute;
    right: 5px;
    top: 75px;
    text-align: center;
    line-height: 50px;
    font-size: 16px;
    z-index: 11;
    cursor: pointer;
    .triangle {
      width: 20px;
      height: 20px;
      background: #fff;
      position: absolute;
      top: -5px;
      left: 65px;
      z-index: -1;
      transform: rotate(45deg);
      -ms-transform: rotate(45deg); /* Internet Explorer */
      -moz-transform: rotate(45deg); /* Firefox */
      -webkit-transform: rotate(45deg); /* Safari 和 Chrome */
      -o-transform: rotate(45deg); /* Opera */
    }
    .img {
      width: 14px;
      height: 16px;
      line-height: 16px;
      position: absolute;
      top: 17px;
      left: 24px;
    }
  }
}
.ON_login {
  width: 510px;
  height: 240px;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: #ffffff;
  border-radius: 16px;
  display: flex;
  justify-content: center;
  .neirong {
    width: 398px;
    height: 137px;
    margin-top: 71px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    .shang {
      width: 340px;
      height: 33px;
      text-align: center;
      line-height: 33px;
      font-size: 24px;
      color: #333333;
    }
    .xia {
      width: 100%;
      height: 48px;
      border-radius: 8px;
      background: linear-gradient(to right, #ee685a, #e25177);
      font-size: 24px;
      color: #ffffff;
      text-align: center;
      line-height: 48px;
      cursor: pointer;
    }
  }
}
.Login_Pop_Up {
  position: fixed;
  width: 510px;
  height: 660px;
  background: url("../../assets/login_beijing.png") no-repeat;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  justify-content: center;
  align-items: center;
  .close {
    position: absolute;
    right: 24px;
    top: 24px;
    width: 14px;
    height: 14px;
    cursor: pointer;
  }
  .zhonxin {
    width: 399px;
    height: 630px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;

    .title {
      height: 33px;
      font-size: 24px;
      color: #333333;
      line-height: 33px;
      margin-top: 10px;
    }
    .form-tiele {
      width: 100%;
      height: 230px;
      // background: #3385FD;
    }
    .OR {
      width: 100%;
      height: 24px;
      margin-top: 38px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        width: 171px;
        height: 1px;
        background: #eeeeee;
      }
      .in {
      }
      .right {
        width: 171px;
        height: 1px;
        background: #eeeeee;
      }
    }
    .choice {
      width: 100%;
      height: 200px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      font-size: 16px;
      color: #ffffff;
      .shang {
        width: 100%;
        height: 42px;
        background: #3275f6;
        border-radius: 8px;
        cursor: pointer;
        display: flex;
        justify-content: center;
        align-items: center;
        position: relative;
        line-height: 42px;
        .fb-signin-button {
          display: inline-block;
          border-radius: 8px;
          width: 100%;
          height: 100%;
          text-align: center;
        }
        .left {
          width: 28px;
          height: 28px;
          position: absolute;
          top: 7;
          left: 12px;
        }
      }
      .zhong {
        width: 100%;
        height: 42px;
        background: #ffffff;
        border-radius: 8px;
        border: 2px solid #dddddd;
        cursor: pointer;
        display: flex;
        justify-content: center;
        align-items: center;
        position: relative;
        line-height: 42px;
        .g-signin-button {
          display: inline-block;
          border-radius: 8px;
          color: #fff;
          width: 100%;
          height: 100%;
          text-align: center;
          color: #333333;
        }
        .left {
          width: 28px;
          height: 28px;
          position: absolute;
          top: 7;
          left: 12px;
        }
      }
      .xia {
        width: 100%;
        height: 42px;
        background: #000000;
        border-radius: 8px;
        cursor: pointer;
        display: flex;
        justify-content: center;
        align-items: center;
        position: relative;
        line-height: 42px;
        .left {
          width: 28px;
          height: 28px;
          position: absolute;
          top: 7;
          left: 12px;
        }
      }
    }
  }
  .tips {
    width: 349px;
    height: 34px;
    color: #999999;
    font-size: 12px;
    text-align: center;
  }
}
</style>
